package com.cdgeekcamp.redas.wechat.factory;

import com.cdgeekcamp.redas.wechat.entity.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

@Component
public class MessageFactory {
    HashMap<String, Type> msgTypeClassMap;

    public MessageFactory() {
        msgTypeClassMap = new HashMap<>();
        msgTypeClassMap.put("event", EventMessageEntity.class);
        msgTypeClassMap.put("image", ImageMessageEntity.class);
        msgTypeClassMap.put("link", LinkMessageEntity.class);
        msgTypeClassMap.put("location", LocationMessageEntity.class);
        msgTypeClassMap.put("shortvideo", ShortVideoMessageEntity.class);
        msgTypeClassMap.put("text", TextMessageEntity.class);
        msgTypeClassMap.put("video", VideoMessageEntity.class);
        msgTypeClassMap.put("voice", VoiceMessageEntity.class);
    }

    public Type getBean(Map<String, String> msgMap) throws Exception {
        String msgTypeDesc = msgMap.get("MsgType");

        if (msgTypeClassMap.get(msgTypeDesc) == null){
            // 不明消息的处理类
            return UnknownMessageEntity.class;
        }

        return msgTypeClassMap.get(msgTypeDesc);
    }
}
